<?php
/**
 * Project: Recipe
 * File: JsonDataAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-26
 * Time: 上午10:44
 */

	class JsonDataAction extends CommonAction{



		function returnrecipecategorys(){

			$recipecategory = M('RecipeCategory');
			$list = $recipecategory->field('id,name')->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
			echo json_encode($data);
		}

		function returnspecialcategorys(){

			$specialcategory = M('SpecialCategory');
			$list = $specialcategory->field('id,name')->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
			echo json_encode($data);
		}

		function returnsaucecategorys(){

			$saucecategory = M('SauceCategory');
			$list = $saucecategory->field('id,name')->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
			echo json_encode($data);
		}

		function returnkitchencategorys(){

			$kitchencategory = M('KitchenCategory');
			$list = $kitchencategory->field('id,name')->select();
			$data = array();
			foreach($list as $key=>$value){
				$data[] = $value;
			}
			echo json_encode($data);
		}

		function getmains(){

			$foodarticle = M('FoodArticle');

			$map['sectionid'] = array('neq',1);
			$map['name'] = array('like',$_GET['query'].'%');
			$foodarticleinfo = $foodarticle->field('id,name,sectionid,categoryid')->where($map)->limit('10')->select();

			$results = array();
			$results['query'] = $_GET['query'];
			foreach($foodarticleinfo as $key=>$value){
				$results['suggestions'][] = $value['name'];
				$results['data'][] = $value['sectionid'].'|'.$value['categoryid'].'|'.$value['id'];
			}

			if(empty($results['suggestions']) || empty($results['data'])){
				$results['suggestions'][] = 'There is no matching content';
				$results['data'][] = 0;
			}
			echo json_encode($results);
		}

		function getmainss(){

			$foodarticle = M('FoodArticle');

		//	$map['sectionid'] = array('neq',1);
			$map['name'] = array('like',$_GET['query'].'%');
			$foodarticleinfo = $foodarticle->field('id,name,sectionid,categoryid')->where($map)->limit('10')->select();

			$results = array();
			$results['query'] = $_GET['query'];
			foreach($foodarticleinfo as $key=>$value){
				$results['suggestions'][] = $value['name'];
				$results['data'][] = $value['sectionid'].'|'.$value['categoryid'].'|'.$value['id'];
			}

			if(empty($results['suggestions']) || empty($results['data'])){
				$results['suggestions'][] = 'There is no matching content';
				$results['data'][] = 0;
			}
			echo json_encode($results);
		}

		function getothers(){

			$foodarticle = M('FoodArticle');

			$map['sectionid'] = array('neq',1);
			$map['name'] = array('like',$_GET['query'].'_%');
			$foodarticleinfo = $foodarticle->field('id,name,sectionid,categoryid')->where($map)->limit('10')->select();

			$results = array();
			$results['query'] = $_GET['query'];
			foreach($foodarticleinfo as $key=>$value){
				$results['suggestions'][] = $value['name'];
				$results['data'][] = $value['sectionid'].'|'.$value['categoryid'].'|'.$value['id'];
			}

			if(empty($results['suggestions']) || empty($results['data'])){
				$results['suggestions'][] = 'There is no matching content';
				$results['data'][] = 0;
			}

			echo json_encode($results);

		}

		function condiments(){
			$foodarticle = M('FoodArticle');

			$map['sectionid'] = array('eq',1);
			$map['name'] = array('like',$_GET['query'].'_%');
			$foodarticleinfo = $foodarticle->field('id,name,sectionid,categoryid')->where($map)->limit('10')->select();

			$results = array();
			$results['query'] = $_GET['query'];
			foreach($foodarticleinfo as $key=>$value){
				$results['suggestions'][] = $value['name'];
				$results['data'][] = $value['sectionid'].'|'.$value['categoryid'].'|'.$value['id'];
			}

			if(empty($results['suggestions']) || empty($results['data'])){
				$results['suggestions'][] = 'There is no matching content';
				$results['data'][] = 0;
			}

			if(!empty($results['data'])){
				echo json_encode($results);
			}
		}


		function getdeals(){
			$deal = D('FoodDeal');
			$map['sectionid'] = $_POST['sectionid'];
			$map['categoryid'] = $_POST['categoryid'];
			$map['articleid'] = $_POST['articleid'];
			$dealinfo = $deal->where($map)->relation(true)->order('deal')->select();
			echo json_encode($dealinfo);
		}

		function calculate(){
			$fooddeal = D('FoodDeal');
			$foodnutritions = M('FoodNutritions');

			$mainsum = array();
			$accessorysum = array();
			$condimentsum = array();
			$totals = array();

			for($mi=0; $mi<count($_POST['mainamount']); $mi++){
				if(empty($_POST['mainamount'][$mi])){
					continue;
				}else{
					$map['sectionid'] = $_POST['mainsectionid'][$mi];
					$map['categoryid'] = $_POST['maincategoryid'][$mi];
					$map['articleid'] = $_POST['mainarticleid'][$mi];
					$map['unit'] = $_POST['maindeal'][$mi];
					$mainfooddealinfo[$mi] = $fooddeal->relation(true)->where($map)->find();
					$foodnutritionsinfo[$mi] = $foodnutritions->where('articleid='.$map['articleid'])->find();
				}

				$mainfooddealinfo[$mi]['sum']=$_POST['mainamount'][$mi]*$mainfooddealinfo[$mi]['deal'];
				$mainfooddealinfo[$mi]['infos'] = $foodnutritionsinfo[$mi];
				unset($mainfooddealinfo[$mi]['infos']['id']);
				unset($mainfooddealinfo[$mi]['infos']['sectionid']);
				unset($mainfooddealinfo[$mi]['infos']['categoryid']);
				unset($mainfooddealinfo[$mi]['infos']['articleid']);

				foreach($mainfooddealinfo[$mi]['infos'] as $key=>$value){
					$mainfooddealinfo[$mi]['infos'][$key] = round($mainfooddealinfo[$mi]['sum']*($value/100),2);
					$mainsum[$key]+=$mainfooddealinfo[$mi]['infos'][$key];
				}

			}


			for($ma=0; $ma<count($_POST['accessoryamount']); $ma++){
				if(empty($_POST['accessoryamount'][$ma])){
					continue;
				}else{
					$mapa['sectionid'] = $_POST['accessorysectionid'][$ma];
					$mapa['categoryid'] = $_POST['accessorycategoryid'][$ma];
					$mapa['articleid'] = $_POST['accessoryarticleid'][$ma];
					$mapa['unit'] = $_POST['accessorydeal'][$ma];
					$accessoryfooddealinfo[$ma] = $fooddeal->relation(true)->where($mapa)->find();
					$foodnutritionsinfo[$ma] = $foodnutritions->where('articleid='.$mapa['articleid'])->find();
				}

				$accessoryfooddealinfo[$ma]['sum']=$_POST['accessoryamount'][$ma]*$accessoryfooddealinfo[$ma]['deal'];
				$accessoryfooddealinfo[$ma]['infos'] = $foodnutritionsinfo[$ma];
				unset($accessoryfooddealinfo[$ma]['infos']['id']);
				unset($accessoryfooddealinfo[$ma]['infos']['sectionid']);
				unset($accessoryfooddealinfo[$ma]['infos']['categoryid']);
				unset($accessoryfooddealinfo[$ma]['infos']['articleid']);

				foreach($accessoryfooddealinfo[$ma]['infos'] as $key=>$value){
					$accessoryfooddealinfo[$ma]['infos'][$key] = round($accessoryfooddealinfo[$ma]['sum']*($value/100),2);
					$accessorysum[$key]+=$accessoryfooddealinfo[$ma]['infos'][$key];
				}

			}

			for($mc=0; $mc<count($_POST['condimentamount']); $mc++){
				if(empty($_POST['condimentamount'][$mc])){
					continue;
				}else{
					$mapc['sectionid'] = $_POST['condimentsectionid'][$mc];
					$mapc['categoryid'] = $_POST['condimentcategoryid'][$mc];
					$mapc['articleid'] = $_POST['condimentarticleid'][$mc];
					$mapc['unit'] = $_POST['condimentdeal'][$mc];
					$condimentfooddealinfo[$mc] = $fooddeal->relation(true)->where($mapc)->find();
					$foodnutritionsinfo[$mc] = $foodnutritions->where('articleid='.$mapc['articleid'])->find();
				}

				$condimentfooddealinfo[$mc]['sum']=$_POST['condimentamount'][$mc]*$condimentfooddealinfo[$mc]['deal'];
				$condimentfooddealinfo[$mc]['infos'] = $foodnutritionsinfo[$mc];
				unset($condimentfooddealinfo[$mc]['infos']['id']);
				unset($condimentfooddealinfo[$mc]['infos']['sectionid']);
				unset($condimentfooddealinfo[$mc]['infos']['categoryid']);
				unset($condimentfooddealinfo[$mc]['infos']['articleid']);

				foreach($condimentfooddealinfo[$mc]['infos'] as $key=>$value){
					$condimentfooddealinfo[$mc]['infos'][$key] = round($condimentfooddealinfo[$mc]['sum']*($value/100),2);
					$condimentsum[$key]+=$condimentfooddealinfo[$mc]['infos'][$key];
				}

			}

//		for($ms=0; $ms<count($mainfooddealinfo); $ms++){
//			foreach($mainfooddealinfo[$ms]['infos'] as $key=>$value){
//				$mainfooddealinfo[$ms]['infos'][$key] = round($mainfooddealinfo[$ms]['sum']*($value/100),2);
//				$mainsum[$key]+=$mainfooddealinfo[$ms]['infos'][$key];
//			}
//		}

			$condiment = array_values($condimentsum);
			$accessory = array_values($accessorysum);
			$main = array_values($mainsum);

			for($t=0; $t<count(array_values($mainsum)); $t++){
				$totals[] = $main[$t]+$accessory[$t]+$condiment[$t];
			}

			echo json_encode(array_values($totals));

		}

		function calculates(){
			$fooddeal = D('FoodDeal');
			$foodnutritions = M('FoodNutritions');

			$mainsum = array();
			$totals = array();

			for($mi=0; $mi<count($_POST['mainamount']); $mi++){
				if(empty($_POST['mainamount'][$mi])){
					continue;
				}else{
					$map['sectionid'] = $_POST['mainsectionid'][$mi];
					$map['categoryid'] = $_POST['maincategoryid'][$mi];
					$map['articleid'] = $_POST['mainarticleid'][$mi];
					$map['unit'] = $_POST['maindeal'][$mi];
					$mainfooddealinfo[$mi] = $fooddeal->relation(true)->where($map)->find();
					$foodnutritionsinfo[$mi] = $foodnutritions->where('articleid='.$map['articleid'])->find();
				}

				$mainfooddealinfo[$mi]['sum']=$_POST['mainamount'][$mi]*$mainfooddealinfo[$mi]['deal'];
				$mainfooddealinfo[$mi]['infos'] = $foodnutritionsinfo[$mi];
				unset($mainfooddealinfo[$mi]['infos']['id']);
				unset($mainfooddealinfo[$mi]['infos']['sectionid']);
				unset($mainfooddealinfo[$mi]['infos']['categoryid']);
				unset($mainfooddealinfo[$mi]['infos']['articleid']);

				foreach($mainfooddealinfo[$mi]['infos'] as $key=>$value){
					$mainfooddealinfo[$mi]['infos'][$key] = round($mainfooddealinfo[$mi]['sum']*($value/100),2);
					$mainsum[$key]+=$mainfooddealinfo[$mi]['infos'][$key];
				}

			}

			$main = array_values($mainsum);

			for($t=0; $t<count(array_values($mainsum)); $t++){
				$totals[] = $main[$t];
			}

			echo json_encode(array_values($totals));

		}


		function cutimg(){
			$time=time();
			$this->cutPic('Small',$time);
			$name=$this->cutPic('Big',$time);
			echo $name;
			//	echo json_encode($name);
		}

		function cutPic($nam,$time){
			unset($_POST['__hash__']);
//		dump($_POST);
			$img=json_decode(str_replace("'",'"',$_POST['picSee'.$nam.'Infor']),true);
//		dump($img);
			$old_img=imagecreatefromstring(file_get_contents($img['url']));
//		dump($old_img);
//		$new_url='./Public/Content/tmp/'.$nam.'_'.$this->getip().'_'.$time.'.jpg';
			$new_url='./Public/Content/tmp/'.$nam.'_'.$time.'.jpg';
			$picname = $nam.'_'.$time.'.jpg';
//		dump($new_url);
			$picname = $time.'.jpg';
			if(function_exists("imagecreatetruecolor")){
				$new_img=imagecreatetruecolor($img['nw'],$img['nh']);
			}else{
				$new_img=imagecreate($img['nw'],$img['nh']);
			}
			imagecopyresampled($new_img,$old_img,0,0,$img['x'],$img['y'],$img['nw'],$img['nh'],$img['w'],$img['h']);
			imagejpeg($new_img,$new_url);
			imagedestroy($new_img);
			return $picname;
//		echo '<img src="'.__ROOT__.'/'.$new_url.'" /><br /><br /><br />';
		}

		/**
		 *菜谱成品图片上传
		 */
		function uploads(){
			import("ORG.Net.emit_upload");
			import("ORG.Util.emit_image");
			$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg','JPG','PNG'),"israndname"=>true,"maxsize"=>20000000));
			if($upload->uploadfiles('pic')){
				$img = new image("./Public/Content/tmp");
				$imgname =  $upload->getnewname();

				$img->thumb($imgname,500,500);

//			if($thname = $img->thumb($imgname, 750, 750,'thumb_')){
//				$img->zdcut($thname,315,445,'product_');
//			}

				//	$img->cut($imgname);
//			if($thname = $img->thumb($imgname, 420, 420,'thumb_')){
//				$img->zdcut($thname,180,240);
//			}

				//	if($img->cut($imgname,180,240)){
				//		$img->thumb($imgname, 210, 210);
				//		$img->thumb($imgname, 70, 70, 'thumb_');
				//	}
				echo $upload->getnewname();
			}else{
				echo $upload->geterrormsg();
			}
		}



	}
